package com.danger.Activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.danger.Model.bean.User;
import com.danger.Presenter.LoginPresenterImpl;
import com.danger.SysApplication;
import com.danger.View.LoginView;

import cn.bmob.v3.Bmob;

public class LoginActivity extends AppCompatActivity implements LoginView, View.OnClickListener{

    private EditText username_editText;
    private EditText password_editText;
    private Button login_button;
    private Button forget_password_button;
    private Button register_button;
    private ProgressDialog progressDialog;
    private LoginPresenterImpl loginPresenterImpl;
    private Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        Bmob.initialize(LoginActivity.this, "583a0d81778d34e5e34b3da53f11da22");
        SysApplication.getInstance().addActivity(this);
        initView();
    }

    private void initView() {
        toolbar = (Toolbar) findViewById(R.id.login_toolbar);
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        loginPresenterImpl = new LoginPresenterImpl(getApplicationContext(), this);
        progressDialog = new ProgressDialog(this, ProgressDialog.THEME_HOLO_LIGHT);
        progressDialog.setMessage("验证登录信息...");
        progressDialog.setCanceledOnTouchOutside(false);
        username_editText = (EditText) findViewById(R.id.username_editText);
        password_editText = (EditText) findViewById(R.id.password_editText);
        login_button = (Button) findViewById(R.id.login_button);
        forget_password_button = (Button) findViewById(R.id.forget_password_button);
        register_button = (Button) findViewById(R.id.register_button);
        login_button.setOnClickListener(this);
        register_button.setOnClickListener(this);
        forget_password_button.setOnClickListener(this);
    }

    @Override
    public void showDialog() {
        progressDialog.show();
    }

    @Override
    public void hideDialog() {
        progressDialog.hide();
        progressDialog.dismiss();
    }

    @Override
    public void showSuccess() {
        Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showError(String error) {
        Toast.makeText(this, "登录失败：" + error, Toast.LENGTH_SHORT).show();
    }

    @Override
    public User getUser() {
        User user = new User();
        user.setUsername(username_editText.getText().toString());
        user.setPassword(password_editText.getText().toString());
        return user;
    }

    @Override
    public void toIndex() {
        startActivity(new Intent(this,IndexActivity.class));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login_button:
                loginPresenterImpl.login();
                break;
            case R.id.register_button:
                startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
                break;
            case R.id.forget_password_button:
                startActivity(new Intent(LoginActivity.this, ForgetPasswordActivity.class));
                break;
            default:
                break;
        }
    }
}
